package com.ruoyi.zhgd.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.zhgd.domain.SysCodeitem;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.SysCodeitemVo;
import com.ruoyi.zhgd.domain.bo.SysCodeitemBo;
import com.ruoyi.zhgd.service.ISysCodeitemService;

/**
 * 参数配置
 *
 * @author cj
 * @date 2023-09-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/codeitem")
public class SysCodeitemController extends BaseController {

    private final ISysCodeitemService iSysCodeitemService;

    /**
     * 查询参数配置列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public R list(String codeid) {
        List<SysCodeitem> list = iSysCodeitemService.queryPageList(codeid);
        return R.ok(list);
    }

    /**
     * 导出参数配置列表
     */
    @SaCheckLogin
    @Log(title = "参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysCodeitemBo bo, HttpServletResponse response) {
        List<SysCodeitemVo> list = iSysCodeitemService.queryList(bo);
        ExcelUtil.exportExcel(list, "参数配置", SysCodeitemVo.class, response);
    }

    /**
     * 获取参数配置详细信息
     *
     * @param pkid 主键
     */
    @SaCheckLogin
    @GetMapping("/{pkid}")
    public R<SysCodeitemVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long pkid) {
        return R.ok(iSysCodeitemService.queryById(pkid));
    }

    /**
     * 新增参数配置
     */
    @SaCheckLogin
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCodeitemBo bo) {
        return toAjax(iSysCodeitemService.insertByBo(bo));
    }

    /**
     * 修改参数配置
     */
    @SaCheckLogin
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCodeitemBo bo) {
        return toAjax(iSysCodeitemService.updateByBo(bo));
    }

    /**
     * 删除参数配置
     *
     * @param pkids 主键串
     */
    @SaCheckLogin
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pkids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] pkids) {
        return toAjax(iSysCodeitemService.deleteWithValidByIds(Arrays.asList(pkids), true));
    }

    /**
    * 取代码列表全部-摊铺层级
    * @author cj
    * @date 2023/10/9 10:00
    * @return
    **/
    @SaCheckLogin
    @GetMapping("getcodelistall")
    public R getCodeListAll(@RequestParam("codeid") String codeid) {
        List<SysCodeitem> list = iSysCodeitemService.getCodeListAll(codeid);
        return R.ok(list);
    }
}
